//
//  GAKKIBaseCollectionViewController+Refresh.m
//  GANGAIKit
//
//  Created by fh on 2019/11/7.
//  Copyright © 2019 cashchina. All rights reserved.
//

#import "GAKKIBaseCollectionViewController+Refresh.h"
#import <objc/runtime.h>

@interface GAKKIBaseCollectionViewController ()
/// 内部分页网络请求参数
@property (nonatomic, readwrite, strong) NSMutableDictionary *requestPageInfoDic;
/// 分页数据模型
@property (nonatomic, readwrite, strong) GAKKIPageInfoModel *pageInfoModel;
@end


@implementation GAKKIBaseCollectionViewController (Refresh)

#pragma mark - Event
/// 下拉刷新方法:子类里需要调用super以处理分页功能
/// @param header 刷新头部控件
- (void)refreshListHeaderAction:(GAKKIBaseRefreshHeader *)header {
    self.pageInfoModel = [[GAKKIPageInfoModel alloc] init];
    DBLog(@"%@",[self.pageInfoModel yy_modelDescription]);
}
/// 上拉加载方法:子类里需要调用super以处理分页功能
/// @param footer 尾部加载控件
- (void)refreshListFooterAction:(GAKKIBaseRefreshFooter *)footer {
    
}

#pragma mark - Public
/// 结束刷新
- (void)endRefresh {
    [self.collectionView.mj_header endRefreshing];
    [self.collectionView.mj_footer endRefreshing];
}

#pragma mark - Setter and getter
/// 是否支持上拉加载
/// @param shouldPullUpToLoadMore 是否支持上拉加载
- (void)setShouldPullUpToLoadMore:(BOOL)shouldPullUpToLoadMore {
    objc_setAssociatedObject(self,@selector(shouldPullUpToLoadMore),[NSNumber numberWithBool:shouldPullUpToLoadMore],OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    // 刷新尾部
    if (!self.collectionView.mj_footer) {
        GAKKIBaseRefreshFooter *footer = [GAKKIBaseRefreshFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshListFooterAction:)]; // GAKKIBaseRefreshFooter / MJRefreshBackStateFooter
        self.collectionView.mj_footer = footer;
    }
    self.collectionView.mj_footer.hidden = !shouldPullUpToLoadMore;
}
- (BOOL)shouldPullUpToLoadMore {
    NSNumber *result = objc_getAssociatedObject(self, _cmd);
    return result.boolValue;
}

/// 是否支持下拉刷新
/// @param shouldPullDownToRefresh 是否支持下拉刷新
- (void)setShouldPullDownToRefresh:(BOOL)shouldPullDownToRefresh {
    objc_setAssociatedObject(self,@selector(shouldPullDownToRefresh),[NSNumber numberWithBool:shouldPullDownToRefresh],OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    // 刷新头部
    if (!self.collectionView.mj_header) {
        GAKKIBaseRefreshHeader *header = [GAKKIBaseRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshListHeaderAction:)]; // GAKKIBaseRefreshHeader / MJRefreshNormalHeader
        self.collectionView.mj_header = header;
    }
    self.collectionView.mj_header.hidden = !shouldPullDownToRefresh;
}
- (BOOL)shouldPullDownToRefresh {
    NSNumber *result = objc_getAssociatedObject(self, _cmd);
    return result.boolValue;
}

- (NSMutableDictionary *)requestPageInfoDic {
    if (!self.pageInfoModel) {
        self.pageInfoModel = [[GAKKIPageInfoModel alloc] init];
    }
    NSMutableDictionary *requestPageInfoDic = [NSMutableDictionary dictionary];
    return requestPageInfoDic;
}
- (void)setRequestPageInfoDic:(NSMutableDictionary *)requestPageInfoDic {
    objc_setAssociatedObject(self,@selector(requestPageInfoDic),requestPageInfoDic,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}


- (GAKKIPageInfoModel *)pageInfoModel {
    return objc_getAssociatedObject(self, _cmd);
}
- (void)setPageInfoModel:(GAKKIPageInfoModel *)pageInfoModel {
    objc_setAssociatedObject(self,@selector(pageInfoModel),pageInfoModel,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end
